package com.yanqu.road.server.gameplayer.module.creditpoints;

import com.yanqu.road.entity.log.LogCreditPoints;
import com.yanqu.road.entity.player.UserCreditPoints;
import com.yanqu.road.logic.bussiness.player.UserCreditPointsBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;

/**
 * 不良积分模块
 */
public class CreditPointsModule extends GeneralModule {

    private UserCreditPoints userCreditPoints;

    public CreditPointsModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userCreditPoints = UserCreditPointsBussiness.getCreditPoints(player.getUserId());
        if (userCreditPoints == null) {
            initData();
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userCreditPoints != null) {
            if (userCreditPoints.isInsertOption()) {
                UserCreditPointsBussiness.addCreditPoints(userCreditPoints);
            } else if (userCreditPoints.isUpdateOption()) {
                UserCreditPointsBussiness.updateCreditPoints(userCreditPoints);
            }
        }
        return true;
    }

    /**
     * 初始
     */
    public void initData() {
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        userCreditPoints = new UserCreditPoints();
        userCreditPoints.setUserId(player.getUserId());
        userCreditPoints.setCreditPoints(0);
        userCreditPoints.setBannedTime(0);
        userCreditPoints.setLastResetTime(zeroTime);
        userCreditPoints.setInsertOption();
    }

    /**
     * 每日重置
     */
    public void resetOneDay() {
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        if (zeroTime > userCreditPoints.getLastResetTime()) {
            userCreditPoints.setCreditPoints(0);
            userCreditPoints.setLastResetTime(zeroTime);
        }
    }

    /**
     * 检测是否禁言
     */
    public boolean checkBanned() {
        return userCreditPoints.getBannedTime() > System.currentTimeMillis();
    }

    /**
     * 解除禁言
     */
    public void removeBanned() {
        long time = System.currentTimeMillis();
        if (userCreditPoints.getBannedTime() > time) {
            userCreditPoints.setBannedTime(time);
        }
        // 清除不良信用分
        userCreditPoints.setCreditPoints(0);
    }


    /**
     * 增加不良信用积分
     *
     * @param level 举报人等级
     */
    public void addCreditPoints(long level) {
        // VIP玩家不会增加不良信用分
        if (VipMgr.getVipLv(player.getUserInfo().getVipExp()) >= GameConfig.CHAT_BLACK_SAFE) {
            return;
        }
        if(player.getUserInfo().isInstructor()){//指导员不加不良信用分
            return;
        }
        long oldBannedTime = userCreditPoints.getBannedTime();
        // 不良信用分公式 => 100*(举报者身份等级/被举报者身份等级)
        long addPoint = ((Double) Math.floor(GameConfig.CHAT_BLACK_POINT_PARAM * (1.0 * level / player.getUserInfo().getTitleId()))).longValue();
        long oldPoint = userCreditPoints.getCreditPoints();
        userCreditPoints.setCreditPoints(userCreditPoints.getCreditPoints() + addPoint);
        // 增加禁言时间 单位秒
        long addTime = 0;
        String[] timeArr = GameConfig.CHAT_BLACK_TIME.split("\\|");
        for (String timeStr : timeArr) {
            String[] split = timeStr.split("=");
            int point = Integer.parseInt(split[0]);
            int time = Integer.parseInt(split[1]);
            if (oldPoint < point && userCreditPoints.getCreditPoints() >= point) {
                addTime += time;
            }
        }
        addBannedTime(addTime);

        long nowTimeMillis = System.currentTimeMillis();
        AutoLogMgr.add(new LogCreditPoints(
                player.getUserId(),
                player.getUserInfo().getRegisterTime() * 1000,
                VipMgr.getVipLv(player.getUserInfo().getVipExp()),
                oldBannedTime,
                (int) addTime,
                oldPoint,
                (int) addPoint,
                userCreditPoints.getCreditPoints(),
                userCreditPoints.getBannedTime() > nowTimeMillis
        ));
    }

    /**
     * 增加禁言时间
     *
     * @param addTime 增加时间(单位/s)
     */
    public void addBannedTime(long addTime) {
        userCreditPoints.setBannedTime(Math.max(userCreditPoints.getBannedTime(), System.currentTimeMillis()) + addTime * 1000);
    }

}
